<template>
  <el-dialog
    :title="!dataForm.categoryCode ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="分类名称" prop="categoryName">
        <el-input v-model="dataForm.categoryName" placeholder="请输入分类名称"></el-input>
      </el-form-item>
      <el-form-item label="父分类CODE" prop="parentCategoryCode"  v-if="categoryVisible">
        <el-input v-model="dataForm.parentCategoryCode" placeholder="请输入父分类CODE" :disabled="!!dataForm.categoryCode"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="clearData()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      categoryVisible: false,
      dataForm: {
        categoryCode: '',
        categoryName: '',
        parentCategoryCode: '',
      },
      dataRule: {
        categoryName: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ],
        parentCategoryCode: [
          { required: true, message: '父分类CODE不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (categoryCode,parentCategoryCode) {
      // 先清空数据，确保初始状态干净
      this.clearData();

      this.dataForm.categoryCode = categoryCode || ''

      if(parentCategoryCode != '') {
        console.log("parenCode"+parentCategoryCode)
        this.dataForm.parentCategoryCode = parentCategoryCode
        this.categoryVisible = true
      } else {
        // 如果没有父分类代码，隐藏该字段
        this.categoryVisible = false
      }

      this.visible = true
      this.$nextTick(() => {
        // 只有在编辑模式下才需要获取数据
        if (this.dataForm.categoryCode) {
          this.$http({
            url: this.$http.adornUrl(`/category/queryWormholeGalaxyCategory`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.categoryCode
            })
          }).then(({data}) => {
            if (data && data.success) {
              this.dataForm.categoryCode = data.data.categoryCode
              this.dataForm.categoryName = data.data.categoryName
              this.dataForm.parentCategoryCode = data.data.parentCategoryCode
            }
          })
        }
        // 不要在设置完值后立即重置表单
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/category/category/${!this.dataForm.categoryCode ? 'createWormholeGalaxyCategory' : 'updateWormholeGalaxyCategory'}`),
            method: 'post',
            data: this.$http.adornData({
              'categoryName': this.dataForm.categoryName,
              'parentCategoryCode': this.dataForm.parentCategoryCode,
            })
          }).then(({data}) => {
            if (data && data.success) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
              // 只在成功提交后才清空数据
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    clearData() {
      this.visible = false
      this.categoryVisible = false
      this.dataForm.categoryCode=''
      this.dataForm.parentCategoryCode=''
      this.dataForm.categoryName=''
    },
  }
}
</script>
